﻿using System.Collections.ObjectModel;

namespace Foundation
{
    public class AutoSyncObservableCollection<T> : ObservableCollection<T>
    {
        public AutoSyncObservableCollection(ObservableCollection<T> observableCollection) :
            base(observableCollection)
        {
            observableCollection.CollectionChanged += (o, e) =>
                {
                    if (e.OldItems != null)
                    {
                        foreach (T item in e.OldItems)
                        {
                            Items.Remove(item);
                        }
                    }

                    if (e.NewItems != null)
                    {
                        foreach (T item in e.NewItems)
                        {
                            Items.Add(item);
                        }
                    }
                };
        }
    }
}
